package com.xgpro.common.utils;

import java.security.Security;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

/**
 * @author tx
 * @version 1.0
 * @date 2019/8/23 22:38
 */
public class MailTool {
	/**
	 * 发送邮件
	 * 
	 * @param to      接收者邮箱
	 * @param subject 邮件主题
	 * @param content 邮件内容
	 * @return success 发送成功 failure 发送失败
	 * @throws Exception
	 */
	public static String sendMail(String to, String subject, String content, String filePath) throws Exception {
		if (to != null) {
			try {
				Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
				final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
				final Properties p = System.getProperties();
				p.setProperty("mail.smtp.host",ReadPropertiesUtil.readUrl("mail_kind"));
				p.setProperty("mail.smtp.auth", "true");
				p.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
				p.setProperty("mail.smtp.socketFactory.fallback", "false");
				// 邮箱发送服务器端口,这里设置为465端口
				p.setProperty("mail.smtp.port", ReadPropertiesUtil.readUrl("mail_port"));
				p.setProperty("mail.smtp.socketFactory.port", ReadPropertiesUtil.readUrl("mail_port"));
				// 根据邮件会话属性和密码验证器构造一个发送邮件的session
				Session session = Session.getInstance(p, new Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(ReadPropertiesUtil.readUrl("mail_username"),
								ReadPropertiesUtil.readUrl("mail_code"));
					}
				});
				/*
				 * Properties props = System.getProperties();
				 * props.put("mail.smtp.host","smtp.126.com"); props.put("mail.smtp.auth",
				 * "true");
				 */
				session.setDebug(true);

				MimeMessage message = new MimeMessage(session);
				message.setFrom(new InternetAddress(ReadPropertiesUtil.readUrl("mail_username")));
				// 邮件名
				message.setSubject(subject);
				// 发送到人
				message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
				// 创建消息部分
				BodyPart messageBodyPart = new MimeBodyPart();
				// 消息
				messageBodyPart.setText(content);
				// 创建多重消息
				Multipart multipart = new MimeMultipart();
				// 设置文本消息部分
				multipart.addBodyPart(messageBodyPart);
				// 附件部分
				messageBodyPart = new MimeBodyPart();
				// 设置要发送附件的文件路径
				DataSource source = new FileDataSource(filePath);
				messageBodyPart.setDataHandler(new DataHandler(source));
				// 处理附件名称中文（附带文件路径）乱码问题
				String[] split = filePath.split("\\/");
				String fileName=split[split.length-1];
				System.out.println(fileName);
				messageBodyPart.setFileName(MimeUtility.encodeText(fileName));
				multipart.addBodyPart(messageBodyPart);
				// 发送完整消息
				message.setContent(multipart);
				// 发送消息
				Transport.send(message);
			} catch (Exception e) {
				e.printStackTrace();
				return "failure";
			}
			return "success";
		} else {
			return "failure";
		}
	}

}
